/** 
 * 
 * 时间戳转时间
 * @author kim
 * @param { dateTime } date -时间戳
 * @param { String } fmt    -格式化格式 
 * formatDate(newTime,'yyyy-MM-dd hh:mm')   //2017-12-27 19:37
 * 
 * */ 

 export function formatDate (date, fmt) {
    date = new Date(date);
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    // 遍历这个对象
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            // console.log(`${k}`)
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
}
// 多久之前
/** 
 * 
 * 时间戳转时间
 * @author kim
 * @param { dateTime } date -时间戳
 * @param { String } fmt    -格式化格式 
 * @param { String } diff    -差值 
 * @param { String } type    -差值类型  year/month/day 
 * formatDateBefore(newTime,'yyyy-MM-dd hh:mm')   //2017-12-27 19:37
 * 注：原则上diff不允许为负数
 * */ 
export function formatDateBefore (date,fmt,diff,type) {
    let month;
    if(type === "day"){
        date = new Date(date - diff*24*60*60*1000)
    }else{
        date = new Date(date);
    }
    // diff < 0 
    if(diff < 0){
        if (/(y+)/.test(fmt)) {
            let afterDiff 
            month = date.getMonth()*1 + 1
            if(type === "month"){
                // diff后的月份
                if(date.getMonth()*1 + 1 - diff*1 > 12){
                    afterDiff = - parseInt((date.getMonth()*1 + 1 - diff*1)/12)
                }else{
                    afterDiff = 0
                }
                if(date.getMonth()*1 + 1- diff*1 === 12){
                    month = 12
                    afterDiff = 0
                }else{
                    month = (date.getMonth()*1 + 1- diff*1)%12
                    afterDiff = - parseInt((date.getMonth()*1 + 1 - diff*1)/12)
                }
                // diff后的年份差值（+）
            }else if(type === "year"){
                afterDiff = diff
            }else{
                afterDiff = 0
            }
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() - afterDiff*1 + '').substr(4 - RegExp.$1.length));
        }
    }else{
        // diff >= 0
        // 计算年的值
        if (/(y+)/.test(fmt)) {
            let beforeDiff 
            if(type === "year" && date.getMonth() < 11){
                beforeDiff = diff
            }else if(type === "month"){
                // diff >= 12并且取模之后的值大于当前月份
                if(diff%12 - 1 > date.getMonth() && parseInt(diff/12) > 0){
                    beforeDiff = parseInt(diff/12) + 1
                // diff < 12并且取模之后的值大于当前月份
                }else if(diff%12 - 1 > date.getMonth() && parseInt(diff/12) <= 0){
                    beforeDiff = 1
                // diff >= 12并且取模之后的值小于于当前月份
                }else if(diff%12 - 1 < date.getMonth() && parseInt(diff/12) > 0){
                    beforeDiff = parseInt(diff/12)
                // diff < 12并且取模之后的值小于当前月份
                }else if(diff%12 - 1 < date.getMonth() && parseInt(diff/12) <= 0){
                    beforeDiff = 0
                // diff取模之后的值等于当前月份
                }else if(diff%12 - 1 === date.getMonth()*1){
                    beforeDiff = parseInt(diff/12) + 1
                }else{
                    beforeDiff = 0
                }
            }else{
                beforeDiff = 0
            }
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() - beforeDiff + '').substr(4 - RegExp.$1.length));
        }
        // 计算月的值
        if(type === "year" && diff > 0){
            month = (date.getMonth()<11?date.getMonth() + 2:(date.getMonth() - 10))
        }else if(type === "month"){
            diff = diff%12
            if(diff - 1 > date.getMonth()){
                month = date.getMonth()*1 + 13 - diff
            }else if(diff - 1 < date.getMonth()){
                month = date.getMonth()*1 + 1 - diff
            }else{
                month = 12
            }
        }else{
            month = date.getMonth() + 1
        }
    } 
    let o = {
        'M+': month,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    // 遍历这个对象
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            // console.log(`${k}`)
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
}

function padLeftZero (str) {
    return ('00' + str).substr(str.length);
}

/** 
 * 
 * 时间戳转相差时间时间
 * @author kim
 * @update 2019年9月12日
 * @param { begintime } date    -开始时间戳
 * @param { endtime }   fmt     -结束时间戳 
 * formatDate(newTime,'yyyy-MM-dd hh:mm')   //2017-12-27 19:37
 * 
 * */ 
export function selectDay(begintime=new Date().getTime(),endtime=new Date().getTime()){
    let nTime = endtime - begintime;
    let day =Math.floor(nTime/86400000);
    return day;
} 